var dashboard = window.dashboard = { currentCustomer: null };

dashboard.AllHubIcons = [{Name: 'Address', Title: 'Address', Link: 'address', Icon: 'icon-home'}, 
        {Name: 'Relationship', Title: 'Relationship', Link: '', Icon: 'icon-group'}, 
        {Name: 'Notes', Title: 'Notes', Link: '', Icon: 'icon-comments'} ];
dashboard.HubIcons = ['Address', 'Relationship', 'Notes'];

if(window.localStorage.username){
    dashboard.username = window.localStorage.username;
}

if(window.localStorage.HubIcons) {    
    dashboard.HubIcons = JSON.parse(window.localStorage.HubIcons) || [];
}

dashboard.configHubIcons = function(icons){
    this.HubIcons = icons;
    window.localStorage.HubIcons = JSON.stringify(icons);
};

dashboard.getHubIconsSettings = function(){
    var addressOn = _.find(dashboard.HubIcons, function(item){
        return item === 'Address';
    });
    var relationshipOn = _.find(dashboard.HubIcons, function(item){
        return item === 'Relationship';
    });
    var notesOn = _.find(dashboard.HubIcons, function(item){
        return item === 'Notes';
    });
    return {Address: addressOn ? true : false, Relationship: relationshipOn ? true : false, Notes: notesOn ? true : false};
};

dashboard.getHubIcons = function(){
    var hubIcons = [];

    if(!window.localStorage.HubIcons){
        hubIcons = this.AllHubIcons;
    }
    else{
        _.each(this.AllHubIcons, function(icon){
            if(_.any(dashboard.HubIcons, function(item){ return item == icon.Name; })) {
                hubIcons.push(icon);
            }
        });
    }
    return hubIcons;
};

dashboard.getCustomer = function(search) {
    var customer = null;

    var id = parseInt(search);

    if(!_.isNaN(id) && _.isNumber(id)){
        customer = _.find(window.customers, function(customer){
            return customer.Id === id;
        });
    }
    else {
        customer = _.find(window.customers, function(customer){
            return customer.Details.FirstName + ' ' + customer.Details.Surname === search;
        });
    }
    return customer;
}

dashboard.getDateDiff = function(date){
    var totalMonths = moment().diff(date, 'months'),
        years = Math.floor(totalMonths / 12),
        months = totalMonths % 12,
        result = '';
    if(years) 
        result += years + ' years';
    if(months)
        result += (years ? ', ' : '') + months + ' months';
    return result;
};

dashboard.getLabelColor = function(status){
    switch(status.toLowerCase()){
        case 'active': return 'success';
        case 'pending': return 'warning';
        case 'cancelled': return 'important';
        default: return '';
    }
};
            
dashboard.getContactMethodIcon = function(type){
    switch(type.toLowerCase()) {
        case 'call': return 'icon-phone';
        case 'email': return 'icon-envelope';
        case 'text': return 'icon-file-text';
    }
};

dashboard._notify = function(){
    var params = arguments,
        image, title, message, duration = 6000;

    if(arguments.length >= 2){
        var lastParamValue = arguments[arguments.length-1];
        if(_.isNumber(lastParamValue)){
            params = Array.prototype.slice.call(arguments, 0, arguments.length -1);
            duration = lastParamValue;
        }
    }

    var all = params.length === 3,
        titleMessage = params.length === 2,
        onlyMessage = params.length === 1;

    if(all){
        image = params[0];
        title = params[1];
        message = params[2];        
    }else if(titleMessage) {
        title = params[0];
        message = params[1];        
    }else if(onlyMessage){
        message = params[0];
    }

    $.gritter.add({
        title: title,
        text: message,
        image: image,
        sticky: false,
        time: duration,
        class_name: 'gritter-custom'
    });
};

dashboard.notify = function(){
   this._notify.apply(this, arguments);
};

dashboard.notifyWarning = function(title, message){
    this._notify('content/img/warning.png', title, message);
};